var express = require('express')
var { graphqlHTTP } = require('express-graphql')
const { buildSchema } = require('graphql')

// String, Int, Float, Boolean, ID 
// type 定义的就是 对象
// [类型]  [Int]代表整型数组
/*
    type Query {
        rollDice(numDice: Int!, numSides:Int!): [Int]
    }
*/
var schema = buildSchema(`
  type Account {
    name: String
    age: Int
    sex: String
    department: String
    salary(city: String!): Int
  }
  type Query {
    getClassMates(classNo: Int!): [String]
    account(username: String): Account
  }
`)

var root = {
  getClassMates({ classNo }) {
    const obj = {
      31: ['张三', '李四', '王五'],
      61: ['张大三', '李大四', '王大五'],
    }
    return obj[classNo]
  },
  salary: ({ city }) => {
    if (city === '北京' || city === '上海' || city === '广州' || city === '深圳') {
      return 10000
    }
    return 3000
  },
  account({ username }) {
    return {
      name: username,
      sex: 'man',
      age: 18,
      department: '开发部',
      salary: this.salary
    }
  }
}
// 查询语句
/*
{
  getClassMates(classNo:31)
}
*/
var app = express()

app.get('/', (req, res) => {
  res.end('<a href="/graphql">graphql</a>')
})

app.use('/graphql', graphqlHTTP({
  schema: schema,
  rootValue: root,
  graphiql: true // 是否开启调试界面，true是开启，false是不开启
}))

app.use(express.static('public'))

app.listen(4000, () => console.log('server is run on http://localhost:4000'))